import store from '../../store/index.js'

export default {
	common:{
		// 本地接口：ip地址换成本地ipv4
		// baseUrl:'http://10.102.55.106:3000/api',
		// 线上接口
		baseUrl:'http://124.220.20.60:3001/api',
		// baseUrl:'https://www.pandayouping.site/api',
		data:{},
		header:{
			'content-type':'application/x-www-form-urlencoded',
			"Content-Type":"application/json",
		},
		method:'GET',
		dataType:'json'
	},
	request( options={}){
		uni.showLoading({
				title:'加载中'
			})
		options.url=this.common.baseUrl+options.url
		options.data=options.data || this.common.data;
		options.header=options.header || this.common.header;
		options.mothod=options.mothod || this.common.mothod;
		options.dataType=options.dataType || this.common.dataType;
		// 判断是否传入header头的token进行用户是否登录的验证
		if(options.header.token){
			options.header.token=store.state.user.token;
			if(!options.header.token){
				uni.showToast({
					title:'请先登录',
					icon:'none'
				})
				uni.navigateTo({
					url:'/pages/login/login'
				})
			}
		}
		
		
		return new Promise((res,rej)=>{
	
			uni.request({
				...options,
				success:(result)=>{
					if(result.statusCode!=200){
						return rej();
					}
					let data=result.data.data;
					res(data)
					setTimeout(function(){
						uni.hideLoading()
					},500)
				}
			})
		})
	}
}